#!/bin/tcsh -ef
####
#
#############################################################################
#                                                                           #
# Title: Sync with central                                                  #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 08/02/2018                                             #
# Last Modification: 08/02/2018                                             #
# Author...........: focus-em.org                                           #
#                                                                           #
#############################################################################
#
# SORTORDER: 1
#
# MANUAL: This script will syncronize the project settings and the current image with a remote "Focus central" machine, i.e. for later pruning of the dataset, re-processing, etc without burdening the Focus workstation running next to the microscope. IMPORTANT: For this script to work, make sure you have setup SSH login without password (e.g. based on shared RSA key) to the remote machine for a user with permissions to write to the project storage location there.
#
# DISPLAY: rsync_host
# DISPLAY: rsync_user
# DISPLAY: rsync_remote_path
# DISPLAY: rsync_del
# DISPLAY: rsync_large
# DISPLAY: rsync_max_size
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
#
set rsync_host = ""
set rsync_user = ""
set rsync_remote_path = ""
set rsync_del = ""
set rsync_large = ""
set rsync_max_size = ""
set import_target_group = ""
#
#
#$end_vars
#
echo bin_2dx = ${bin_2dx}
echo proc_2dx = ${proc_2dx}
#
#
set scriptname = central_rsync
#
echo "<<@progress: 1>>"
#
source ${proc_2dx}/initialize
#
#
############################################################################# 
${proc_2dx}/linblock "Syncing the current image directory with ${rsync_host}:${rsync_remote_path}"
#############################################################################

set curr_path = `pwd -P`

set del = ""
if ( ${rsync_del} == "y" ) then

	set del = "--delete"

endif

set max_size = ""
echo ":: max_size is ${max_size}"
echo ":: rsync_large is ${rsync_large}"
if ( ${rsync_large} == "n" ) then

	set max_size = "--max-size=${rsync_max_size}G"

endif
echo ":: rsync_large is ${rsync_large}"
echo ":: max_size is ${max_size}"

set group_dir = ${curr_path:h:t}
set proj_dir = ${curr_path:h:h:t}

echo ":: #############################################################################"
echo ":: Synchronizing project files in the merge directory:"
echo ":: rsync -avzhuP ${del} ../../merge ${rsync_user}@${rsync_host}:${rsync_remote_path}/${proj_dir}"
echo ":: #############################################################################"
rsync -avzhuP ${del} ../../merge ${rsync_user}@${rsync_host}:${rsync_remote_path}/${proj_dir} &
#
echo "<<@progress: 5>>"
#
echo ":: #############################################################################"
echo ":: Synchronizing gain-reference and defects list (if present):"
echo ":: rsync -avzhuP ${del} ${max_size} ../../${group_dir}/*.dm4 ../../${group_dir}/*.txt ../../${group_dir}/*.mrc ${rsync_user}@${rsync_host}:${rsync_remote_path}/${proj_dir}/${group_dir}"
echo ":: #############################################################################"
ssh ${rsync_user}@${rsync_host} 'mkdir -p ${rsync_remote_path}/${proj_dir}/${group_dir}'
rsync -avzhuP ${del} ${max_size} ../../${group_dir}
#/*.dm4 ../../${group_dir}/*.txt ../../${group_dir}/*.mrc ${rsync_user}@${rsync_host}:${rsync_remote_path}/${proj_dir}/${group_dir} &
#
echo "<<@progress: 10>>"

echo ":: #############################################################################"
echo ":: Synchronizing image data:"
echo ":: rsync -avzhuP ${del} ${max_size} ../../${group_dir}/${curr_path:t} ${rsync_user}@${rsync_host}:${rsync_remote_path}/${proj_dir}/${group_dir}"
echo ":: #############################################################################"
ssh ${rsync_user}@${rsync_host} 'mkdir -p ${rsync_remote_path}/${proj_dir}/${group_dir}'
rsync -avzhuP ${del} ${max_size} ../${curr_path:t} ${rsync_user}@${rsync_host}:${rsync_remote_path}/${proj_dir}/${group_dir} &

#
echo "<<@progress: 100>>"
#
############################################################################# 
${proc_2dx}/linblock "Sync with central finished correctly!"
#############################################################################
